package ca.swank.hq.hard;

// the Hardquor base
import ca.swank.hardquor.*;

// legacy imports
import java.util.LinkedList;
import java.util.TreeSet;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Properties;
import java.io.*;
import java.util.StringTokenizer;
// for managing URL Resources (usually ZIP files)
import java.net.URLClassLoader;
import java.net.URL;
import java.net.MalformedURLException;

/**
* manages a skin file, which is saved in ZIP format
*
* @author Jesse Wilson
* @version 1.0 10/18/2001
*/
public class HardquorResourceSkin extends HardquorSkin
{
  // watches and waits for images to load
  private MediaTracker tracker;
  // the zip file or folder where all images and properties file are held
  private ClassLoader skinResource;
  // used to track properties
  protected Properties skinProperties;
  // the prefix to all resources in the skinResource
  private String skinPath;

  // the name of the directory where skin files go if stored in local jar
  public static final String JAR_SKIN_PATH = "hardquorskin/";


  /**
   * uses Java .properties mechanism to store properties of skin file
   *
   * if the skinResourceRaw attribute is null, the skin looks in the
   * classloader's JAR file for a skin in the 'hardquorskin' folder
   */
  public HardquorResourceSkin(String skinResourceRaw, JFrame frame)
  { // properties file stores all information for skin
    skinProperties = new Properties();
    // the media tracker for notification of images being loaded
    tracker = new MediaTracker(frame);

    // a skin is being specified as a URL; use that zip resource
    if(skinResourceRaw != null)
    { try
      { // create the skin resource class loader from the URL
  	    skinResource = new URLClassLoader(new URL[] { new URL(skinResourceRaw) } );
  	    skinPath = "";

      } catch(MalformedURLException e)
      { System.out.println("skin file specified by malformed url: " + skinResourceRaw);
        System.exit(1);
      }

    // the skin is not being specified as a URL, use the local JAR as the resource
    } else
    { skinResource = this.getClass().getClassLoader();
      skinPath = JAR_SKIN_PATH;
    }


    // read the properties file from the resource
    try
    { InputStream skinPropertiesInputStream = skinResource.getResourceAsStream(skinPath + SKIN_PROPERTIES);
	  skinProperties.load(skinPropertiesInputStream);

    } catch(Exception e)
    { System.out.println("error reading skin file: " + skinResourceRaw);
      System.exit(1);
    }


    loadSkinAttributes();
    loadSkinImages();
  }


  /**
   * gets the integer of the specified key from the specified properties file
   */
  protected int getInt(String key)
  { String raw = skinProperties.getProperty(key);
    return Integer.parseInt(raw);
  }


  /**
   * gets the integer array from the specified properties file
   */
  protected int[] getIntArray(String key)
  { String raw = skinProperties.getProperty(key);
    StringTokenizer tokens = new StringTokenizer(raw, " ,\t");
    int[] clean = new int[tokens.countTokens()];
    int currentToken = 0;
    while(tokens.hasMoreTokens())
    { clean[currentToken] = Integer.parseInt(tokens.nextToken());
      currentToken++;
    }
    return clean;
  }


  /**
   * gets the String of the specified name
   */
  protected String getString(String key)
  { return skinProperties.getProperty(key);
  }


  /**
   * from Builder.com JAVA email tips, "Learn how to create HTML-like colors in Java"
   * May 2, 2002
   *
   * Given a html-like color such as #bbac33, turns it
   * into a java.awt.Color. The # on the front is optional.
   */
  protected Color getColor(String key)
  { String color = skinProperties.getProperty(key);

    if(color.charAt(0) == '#')
    { color = color.substring(1);
    }

    if(color.length() != 6)
    { return null;
    }

    int r = Integer.parseInt(color.substring(0,2), 16);
    int g = Integer.parseInt(color.substring(2,4), 16);
    int b = Integer.parseInt(color.substring(4), 16);

    return new Color(r, g, b);
  }


  /**
   * gets the image from the resource
   */
  protected Image getImage(String key)
  { String imageFileName = skinProperties.getProperty(key);
    Image loadedImage = Toolkit.getDefaultToolkit().getImage(skinResource.getResource(skinPath + imageFileName));
    tracker.addImage(loadedImage, 0);
    try
    { tracker.waitForID(0);
    } catch(InterruptedException e)
    { System.out.println("Interupted while loading image named: " + imageFileName);
    }
    return loadedImage;
  }

}